package com.kdtech.analyse.Bbs.common;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 动网论坛 dvbbs 解析模板
 * @author Administrator
 *
 */
public class BbsAnalyseDvbbs implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		url = UrlArgumentTop.FromatUrl(url);
		url = StringUtils.substringAfterLast(url, "/");
		String[] regex = {
				"dispbbs.asp\\?boardid=[0-9]*&id=[0-9]*",
				"dispbbs_[0-9]*_[0-9]*_[0-9]*_skin1.html",
				"dispbbs-[0-9]+.htm",
				"dispbbs.asp\\?boardid=[0-9]*&Id=[0-9]*",
				"dispbbs_[0-9]*_[0-9]*.html",
				"read-html-tid-[0-9]*,[0-9]*,1.html",
				"read-html-tid-[0-9]*.html",
				"read-html-tid-[0-9]*-ds-1.html",
				"dispbbs.asp\\?id=[0-9]*",
				"dispbbs.asp\\?boardID=[0-9]*&ID=[0-9]*&page=1",
		};
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
			return null;
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		String commnetNum = null;
		String clickNum = null;
		Document doc = Jsoup.parse(htmltxt);
		String author=null;
		doc.select("a:containsOwn(登录/注册后可看大图)").remove();
		/*
		 * 标题
		 */
		title = doc.select("h1").text();
		if(title.startsWith("主题：")){
			title = title.substring("主题：".length());
		}
		if(StringUtils.isBlank(title))
			title = doc.select("td.inforight b").text();
		if(StringUtils.isBlank(title))
		{
			Elements titleList = doc.select("div.th > div");
			if(titleList.size()>1)
			{
				title = titleList.get(1).text();
			}
		}
		
		
		/*
		 * 日期
		 */
		dateStr = doc.select("td.inforight span.font10").text();
		if(dateStr == null)
			dateStr = doc.select("span.font10").text();
		date = DateUtils.matchDate(dateStr);

		if(date == null){
			dateStr = doc.select("table.bbslist.border td.usermenu").text();
			date = DateUtils.matchDate(dateStr);
		}
		if(date==null)
		{
			date = DateUtils.matchDate(doc.select("div.tipTop").text());
		}
		if(date==null)
		{
			date = DateUtils.matchDate(doc.select("div.postbottom1").text());
		}
		
		/*
		 * 内容
		 */
		Element firstDiv = doc.select("table.bbslist").first();
		if(firstDiv!=null){
			content = HtmlCleaner.getContentHtml(url,firstDiv.select("div#textstyle_1"));
			author = firstDiv.select("span.username b").text();
		}
		if(StringUtils.isBlank(content))
		{
			content = HtmlCleaner.getContentHtml(url,doc.select("div#read_tpc"));
		}
		if(StringUtils.isBlank(content))
		{
			Elements contentList = doc.select("div.post > div");
			if(contentList.size()>2)
			{
				content = HtmlCleaner.getContentHtml(url,contentList.get(2));
			}
		}
		
		/*
		 * 作者
		 */
		Elements authorList = doc.select("div.readName");
		if(authorList.size() > 0)
		{
			author = authorList.get(0).text();
		}
		if(StringUtils.isBlank(author))
		{
			authorList = doc.select("div.postuserinfo div");
			if(authorList.size()>1)
			{
				author = authorList.get(0).text();
			}
		}
		
		clickNum = doc.select("table.topic span b").text();
			NewsMeta news = new NewsMeta();
			news.setUrl(url);
			news.setType(4);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.matchNumber(commnetNum));
			news.setClickNum(NumberUtils.matchNumber(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setAuthor(author);
			news.setUpdateUrl(url);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	

}
